package com.doublefish.doublefishrobot.ui.home.s6;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.DeviceCheckActivity;
import com.doublefish.doublefishrobot.ui.home.bean.DeviceBean;
import com.doublefish.doublefishrobot.ui.home.inerface.OnModeMenuRightBtnClickListner;
import com.doublefish.doublefishrobot.utils.ble.DFBleManager;

/**
 * 作者:dugaofeng on 2024/12/20 08:36
 */
public class S6ModeMenuFragment extends Fragment implements View.OnClickListener{

    private View view;

    private View backLL;
    private TextView titleTv;
    private TextView rightTv;

    private OnModeMenuRightBtnClickListner onModeMenuRightBtnClickListner;
    private String title = null;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.activty_s6_menu, container, false);

            initViews();
        }
        return view;
    }


    private void initViews() {
        backLL = view.findViewById(R.id.ll_back);
        backLL.setVisibility(View.INVISIBLE);

        titleTv = view.findViewById(R.id.tv_title);
        DeviceBean deviceBean = DFBleManager.getInstance().getBleDeviceBean();
        if (deviceBean != null) {
            title = deviceBean.getAlias();
        }
        if (title == null) {
            titleTv.setText(R.string.super_6_robot);
        } else {
            titleTv.setText(title);
        }

        rightTv = view.findViewById(R.id.tv_right);
        rightTv.setText(R.string.disconnect );
        rightTv.setOnClickListener(this);

        TextView normal = view.findViewById(R.id.tv_mode_normal);
        TextView train = view.findViewById(R.id.tv_mode_train);
        TextView custom = view.findViewById(R.id.tv_mode_custom);
        TextView checkTv = view.findViewById(R.id.tv_init_check);;

        normal.setOnClickListener(this);
        train.setOnClickListener(this);
        custom.setOnClickListener(this);
        checkTv.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_mode_normal:
                toS6ModeNormalActivity();
                break;
            case R.id.tv_mode_train:
                toS6ModeTrainListActivity();
                break;
            case R.id.tv_mode_custom:
                toS6ModeCustomListActivity();
                break;

            case R.id.tv_init_check:
                toInitCheckActivity();
                break;

            case R.id.tv_right:
                onRightTvClick();
                break;
        }
    }

    public void disConnect() {
        onRightTvClick();
    }

    private void onRightTvClick() {
        DFBleManager.getInstance().disConnect();
        if (this.onModeMenuRightBtnClickListner != null) {
            this.onModeMenuRightBtnClickListner.onModeMenuRightBtnClick();
        }
    }

    private void toInitCheckActivity() {
        DeviceBean deviceBean = DFBleManager.getInstance().getBleDeviceBean();
        Intent intent = DeviceCheckActivity.makeIntent(getActivity(), deviceBean);
        startActivity(intent);
    }

    private void toS6ModeNormalActivity() {
        Intent intent = new Intent(getActivity(), S6ModeNormalActivity.class);
        startActivity(intent);
    }

    private void toS6ModeTrainListActivity() {
        Intent intent = new Intent(getActivity(), S6ModeTrainListActivity.class);
        startActivity(intent);
    }

    private void toS6ModeCustomListActivity() {
        Intent intent = new Intent(getActivity(), S6ModeCustomListActivity.class);
        startActivity(intent);
    }

    public void setOnModeMenuRightBtnClickListner(OnModeMenuRightBtnClickListner onModeMenuRightBtnClickListner) {
        this.onModeMenuRightBtnClickListner = onModeMenuRightBtnClickListner;
    }
}

